package com.xcity.game.combat.buff;

import com.alibaba.fastjson.annotation.JSONField;
import com.xcity.game.combat.Action;
import com.xcity.game.combat.ActionEffect;
import com.xcity.game.combat.Round;
import com.xcity.game.combat.unit.CombatUnit;

/**
 * 战斗buff
 * @author yang.li
 *
 */
public abstract class CombatBuff implements ActionEffectListener {
	
	public static final byte STATE_REMOVED = 0, STATE_ADDED = 1;
	public static final int ROUNDS_NO_LIMIT = -1;
//	protected static final AtomicInteger IDS = new AtomicInteger();
	
	protected CombatBuffTemplate template;
	protected CombatUnit owner;
	protected byte state;
	protected int leftRounds;
	protected int endRound = ROUNDS_NO_LIMIT;
	protected boolean active; // 是否激活，未激活不计算回合数，默认不激活
	
	public CombatBuff(CombatBuffTemplate template, CombatUnit owner) {
		this(template, owner, ROUNDS_NO_LIMIT);
	}
	
	public CombatBuff(CombatBuffTemplate template, CombatUnit owner, int maxRounds) {
		this.template = template;
		this.owner = owner;
		this.state = STATE_ADDED;
		this.leftRounds = maxRounds;
	}
	
	public void setState(byte state) {
		this.state = state;
	}

	public byte getState() {
		return state;
	}
	
	public String getIcon() {
		return state != STATE_REMOVED ? template.getIcon() : null;
	}
	
	public int getId() {
		return template.getId();
	}
	
	public long getOwnerId() {
		return owner.getId();
	}
	
	public int getEndRound() {
		return endRound;
	}

	@JSONField(serialize = false)
	public CombatUnit getOwner() {
		return owner;
	}
	
	@JSONField(serialize = false)
	public CombatBuffTemplate getTemplate() {
		return template;
	}
	
	public void uneffect() {
		this.state = STATE_REMOVED;
//		owner.removeBuff(this);
	}
	
	@Override
	public void roundStart(Round round) {
	}

	@Override
	public CombatUnit preTarget(Action action) {
		return null;
	}

	@Override
	public void preDamage(ActionEffect effect) {
	}

	@Override
	public void afterDamage(ActionEffect effect) {
	}

	@Override
	public void preDamage0(ActionEffect effect) {
	}

	@Override
	public void afterDamage0(ActionEffect effect) {
	}

	@Override
	public void roundEnd(Round round) {
		if (leftRounds != ROUNDS_NO_LIMIT) {
			if (active && state == STATE_ADDED && leftRounds > 0 && --leftRounds <= 0) {
				uneffect();
				round.addBuff(this);
			}
			this.endRound = round.getRound() + leftRounds;
		}
	}

	@Override
	public void preAction(Action action) {
	}

	@Override
	public void afterAction(Action action) {
	}
	
	@JSONField(serialize = false)
	public boolean isDebuff() {
		return false;
	}
	
	@Override
	public CombatBuff clone() {
		return new CloneableBuff(this);
	}
	
}
